package com.mar.framecompress;

import java.util.EnumMap;
import java.util.Hashtable;
import java.util.Map;

import vnc.server.rfb.cache.CachedEncoderDecoder;

import com.mar.framecompress.impl.RawEncoderDecoder;
import com.mar.framecompress.impl.test.EncodeDecodeTest;
import com.mar.framecompress.impl.zlib.ZlibEncoderDecoder;

public class DecoderRegistry {
	private Map<Encoding, IDecoder> frameDecoderStore = new EnumMap<Encoding, IDecoder>(Encoding.class);
	
	private Map<PixelFormat, IColorDecoder> colorDecoderStore = new Hashtable<PixelFormat, IColorDecoder>(); 
	
	public void registerDecoder( IDecoder Decoder, Encoding type ){
		frameDecoderStore.put(type, Decoder);
	}
	
	public void unregisterDecoder( Encoding type ){
		frameDecoderStore.remove(type);
	}
	
	public void registerColorDecoder( IColorDecoder Decoder, PixelFormat pixelFormat ){
		colorDecoderStore.put(pixelFormat, Decoder);
	}
	
	public void unregisterColorDecoder( PixelFormat pixelFormat ){
		colorDecoderStore.remove(pixelFormat);
	}

	public IDecoder get(Encoding key) {
		return frameDecoderStore.get(key);
	}

	public IColorDecoder get(PixelFormat key) {
		return colorDecoderStore.get(key);
	}

	// SINGLETON IMPL
	private static final DecoderRegistry INTANCE = new DecoderRegistry();
	private DecoderRegistry(){
		ZlibEncoderDecoder zlibEncoderDecoder = new ZlibEncoderDecoder();
		registerDecoder( zlibEncoderDecoder, Encoding.ZLIB );

		CachedEncoderDecoder cachedEncoderDecoder = new CachedEncoderDecoder();
		CompositeDecoder compositeDecoder = new CompositeDecoder( zlibEncoderDecoder, cachedEncoderDecoder );
		registerDecoder( compositeDecoder , Encoding.DEFAULT);
	};
	public static DecoderRegistry getInstance(){ return INTANCE; }
}
